home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / IPFlakeWay / Squelch Module / source / NatTable.h < prev    next >
Text File  |  2000-06-23  |  7KB  |  169 lines

  1. // =================================================================================
  2. //    NatTable.h                     ©1997 Sustainable Softworks. All rights reserved.
  3. // =================================================================================
  4. //    Network Address Translation table
  5. //    This module encapsulates the IP network address translation table
  6. //    as a set of cover routines to hide the internal structure and algorithm
  7. //    used to access the table.
  8. //
  9. //    Since this module executes in the STREAMs context, instance data is
  10. //    allocated when the STREAM is opened rather than by instantiating
  11. //    a C++ object.  For this reason, each of the module functions is
  12. //    passed a pointer to its instance data.
  13.  
  14. #ifndef _H_NatTable
  15. #define _H_NatTable
  16. #pragma once
  17.  
  18. #include <types.h>
  19. #include "MyTypes.h"
  20.  
  21. // include ProxyModule.h before this file
  22. struct translationEntry {            // size is 40 bytes
  23.     xEndpoint_t    actual;                // the actual endpoint on the private LAN
  24.     UInt16        portRange;            // number of ports in port range
  25.     xEndpoint_t    apparent;            // the external visible endpoint
  26.     UInt16        identification;        // from IP header
  27.     UInt16        fragmentOffset;
  28.     UInt32        seqInitial;            // used to offset seq and ack #'s
  29.     SInt16        seqOffset;            //   for content masquerading
  30.     UInt32        seqInitial2;        // used to offset seq and ack #'s
  31.     SInt16        seqOffset2;            //   for content masquerading
  32.     SInt16        seqOffsetPrev;
  33.     UInt16        age;                // timer intervals since table entry last refreshed
  34.     UInt16        flags;                // track entry type and state
  35.     UInt16        entryID;            // index used to identify a specific table entry
  36.     UInt8        protocol;            // from IP header (TCP, UDP, ICMP,...)
  37.     UInt8        pad;                // pad to 40 bytes
  38. };
  39. typedef struct translationEntry translationEntry_t;
  40.  
  41. // Values for NAT entry Flags
  42. #define kFlagPermanent            1        // entry is permanent and cannot be aged out
  43. #define kFlagFINLocal            2        // Seen TCP FIN from local host
  44. #define kFlagFINPeer            4        // Seen TCP FIN from peer
  45. #define kFlagActive                8        // Entry contains valid data
  46. #define kFlagStatic                0x10    // Apparent EP is static
  47. #define kFlagNoRestore            0x20    // Don't restore this entry
  48. #define kFlagDNSForwarding        0x40    // DNS Forwarding Entry
  49. #define kFlagNonSyn                0x80    // Sent more than a Syn
  50. #define kFlagWamnet                0x0100    // WAM!NET private
  51.  
  52. // Values for protocol
  53. #define kProtocolNone        0
  54. #define kProtocolAny        0
  55. #define kProtocolICMP        1
  56. #define kProtocolTCP        6
  57. #define kProtocolUDP        17
  58. #define kProtocolGRE        47
  59.  
  60. #define kNatTableDim        512
  61. #define    kPortTableDim        16
  62. #define kPortTableSize        512
  63. // Entry time out in RIP intervals
  64. #define kTimeOutTCP            60        // Default TCP (30 mins)
  65. #define kTimeOut4M            8        // 4 minutes
  66. #define kTimeOut2M            5        // 2 minutes
  67. #define kTimeOut1M            2        // 30-60 seconds
  68.  
  69. // The first entry of the Table is used for table bookkeeping 
  70. struct tableData {
  71.     UInt32    portNameHash;    // hashed name of port to masquerade on
  72.     UInt32    exposedHost;    // exposed host address
  73.     NetNumber_t    natNetwork;    // the apparent IP address to use
  74.     NetNumber_t actualNetwork;    // actual network that should not be translated
  75.     UInt16    lastEntry;        // index of last physical entry in table
  76.     UInt16    lastUsed;        // index of last logical entry in table
  77.     UInt16    firstActive;    // start of active list
  78.     UInt16    firstDeleted;    // start of deleted list
  79.     UInt16    cacheActual;    // remember result of last FindActual
  80.     UInt16    cacheApparent;    // remember result of last FindApparent
  81.     UInt16    mssClamp;        // TCP mss limit
  82.     Boolean    isNatOn;        // enable/disable NAT
  83. };
  84. typedef struct tableData tableData_t;
  85.  
  86. // Design Notes:
  87. // We include additional information beyond the endpoint (address, port)
  88. // translation pair to reduce the need for port multiplexing.
  89. // As long as the actual endpoint can be uniquely identified, we can
  90. // re-use the same port numbers to reduce our compatibility risk.
  91. //
  92. // A table entry with port number 0 means match any port (no port translation).
  93. // A table entry with protocol 0 means match any protocol.
  94. // A table entry with peer 0 means match any peer.
  95. // WriteEntry with entryID zero means create a new entry.
  96. //
  97. //    For efficiency, the NAT table is threaded by a series of links
  98. //    using the entryID.  The entryID of each table entry points to
  99. //    the next entry in the thread or zero for end of list.
  100. //    Two threads are kept, one for active entries, and another for
  101. //    deleted entries waiting to be re-used.
  102. //
  103. //    When a found entry is returned, the entryID parameter is the index
  104. //    of that entry.  This index allows specific entries to be modified
  105. //    without searching.
  106. //
  107. //  If a port is already in use, we try to allocate the next
  108. //  sequentially available unused port using a bitmap to
  109. //  to track the ports in use.
  110.  
  111. // ---------------------------------------------------------------------------
  112. // forward function declarations [external]
  113. // ---------------------------------------------------------------------------
  114.  
  115. void InitTable(translationEntry_t table[]);
  116. // Initialize the NAT table to be empty.
  117.  
  118. void SetPortName(translationEntry_t table[], UInt32 inPortNameHash);
  119. // Set hashed port name to determine which interface this table is for.
  120.  
  121. UInt32 GetPortName(translationEntry_t table[]);
  122. // Get hashed port name to determine which interface this table is for.
  123.  
  124. void SetNatNetwork(translationEntry_t table[], NetNumber_t* inNatNetwork);
  125. NetNumber_t* GetNatNetwork(translationEntry_t table[]);
  126.  
  127. void SetActualNetwork(translationEntry_t table[], NetNumber_t* inActualNetwork);
  128. NetNumber_t* GetActualNetwork(translationEntry_t table[]);
  129.  
  130. void SetExposedHost(translationEntry_t table[],  UInt32 inExposedHost);
  131. UInt32 GetExposedHost(translationEntry_t table[]);
  132.  
  133. void SetMSSClamp(translationEntry_t table[],  UInt16 inMSSClamp);
  134. UInt16 GetMSSClamp(translationEntry_t table[]);
  135.  
  136. void SetNatOn(translationEntry_t table[], Boolean inNatOn);
  137. Boolean GetNatOn(translationEntry_t table[]);
  138.  
  139. Boolean FindApparentEndpoint(translationEntry_t table[], translationEntry_t* mapData);
  140. // Look for a matching NAT table entry, if none found
  141. // create one.  Use a different port number only if
  142. // necessary.  Resets the entry Age to zero.
  143. Boolean FindApparentDNSEndpoint(translationEntry_t table[], translationEntry_t* mapData, UInt32 destAddr);
  144.  
  145. Boolean FindActualEndpoint(translationEntry_t table[], translationEntry_t* mapData);
  146. // Look for matching entry.  If no complete match, look
  147. // for unambigeous partial match.  Return false if none found.
  148.  
  149. UInt16 FindEntry(translationEntry_t table[], translationEntry_t* mapData);
  150. // Return index of table entry with matching data:
  151. //    apparent endpoint
  152. //    actual endpoint
  153. //    protocol
  154.  
  155. Boolean WriteEntry(translationEntry_t table[], translationEntry_t* mapData);
  156. // Update the flags or other data associated with a NAT table entry.
  157. // To delete an entry, the corresponding flag is set.
  158. // To load a new entry, use entryID=0.  Returns false if table is full.
  159.  
  160. void    DeleteEntry(translationEntry_t table[], UInt16 index, UInt16 prev);
  161.  
  162. UInt32 AgeTable(translationEntry_t table[], UInt16 timeOut);
  163. //  Age every NAT entry.  Delete any that exceed timeOut.
  164. //  Could be invoked by ioctl from IPNLink application to
  165. //  keep the table up to date.  Return the number of active
  166. //  entries in the table (so we can track if the table becomes
  167. //  full, and when entries are aged out).
  168.  
  169. #endif